package com.base.cn.platform.os.service.op.groom;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.op.groom.OpGroomPlaceDao;
import com.base.cn.platform.os.entity.op.groom.OpGroomData;
import com.base.cn.platform.os.entity.op.groom.OpGroomPlace;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 推荐位置Biz
 *
 * Created by ll on 2018/05/07.
 */
@Service
public class OpGroomPlaceBiz  extends BaseBiz<OpGroomPlace,OpGroomPlaceDao>{
    @Autowired
    private OpGroomDataBiz opGroomDataBiz;

    /**
     * 搜索位置列表
     * @param page
     * @param groomPlace
     * @return
     */
    public PageInfo<OpGroomPlace> queryGroomPlaceListPage(Pagination page, OpGroomPlace groomPlace){
        String whereSql = whereGroomPlaceSql(groomPlace);
        return this.findPage(whereSql,page,null);
    }


    /**
     * 搜索位置列表
     * @param groomPlace
     * @return
     */
    public List<OpGroomPlace> queryGroomPlaceList(OpGroomPlace groomPlace){
        String whereSql = whereGroomPlaceSql(groomPlace);
        return this.find(whereSql,null,null);
    }

    /**
     * 搜索所推荐位置根据唯一Key
     * @param placeKey
     * @return
     */
    public OpGroomPlace queryGroomPlaceByPlaceKey(String placeKey){
        OpGroomPlace opGroomPlace = new OpGroomPlace();
        opGroomPlace.setPlaceKey(placeKey);
        String whereSql = whereGroomPlaceSql(opGroomPlace);
        return this.findOne(whereSql,null);
    }

    /**
     * 根据Ids搜索推荐位置
     * @param groomPlaceIds
     * @return
     */
    public List<OpGroomPlace> queryGroomPlaceListByIds(String groomPlaceIds){
        if (StringUtils.isNotEmpty(groomPlaceIds)){
            OpGroomPlace opGroomPlace = new OpGroomPlace();
            groomPlaceIds = StringUtils.subHeadTailString(groomPlaceIds,",");
            opGroomPlace.setPlaceIds(groomPlaceIds);
            String whereSql = whereGroomPlaceSql(opGroomPlace);
            return this.find(whereSql,null,null);
        }
        return null;
    }
    /**
     * 条件搜索语句
     * @param groomPlace
     * @return
     */
    public String  whereGroomPlaceSql(OpGroomPlace groomPlace){
        StringBuilder whereSql = new StringBuilder("  1=1");
        if(ObjectUtils.isNotEmpty(groomPlace)){
            if(StringUtils.isNotEmpty(groomPlace.getPlaceName())){
                whereSql.append(" and placeName like concat('%','").append(groomPlace.getPlaceName()).append("','%')");
            }
            if(StringUtils.isNotEmpty(groomPlace.getPlaceKey())){
                whereSql.append(" and placeKey ='").append(groomPlace.getPlaceKey()).append("'");
            }
            if(StringUtils.isNotEmpty(groomPlace.getDepict())){
                whereSql.append(" and depict like concat('%','").append(groomPlace.getDepict()).append("','%')");
            }
            if(StringUtils.isNotEmpty(groomPlace.getDataType())){
                whereSql.append(" and dataType like concat('%','").append(groomPlace.getDataType()).append("','%')");
            }
            if(StringUtils.isNotEmpty(groomPlace.getPlaceIds())){
                String placeIds = StringUtils.subHeadTailString(groomPlace.getPlaceIds(),",");
                whereSql.append(" and id in(").append(placeIds).append(")");
            }
            if(groomPlace.getStatus()!=null&&groomPlace.getStatus()>0){
                whereSql.append(" and status =").append(groomPlace.getStatus());
            }
        }
        whereSql.append(" order by id desc");
        return whereSql.toString();
    }

    /**
     * 保存修改推荐位置
     * @param groomPlace
     * @return
     */
    public OpGroomPlace saveUpdateGroomPlace(OpGroomPlace groomPlace){
        if(DataUtil.idIsNotNull(groomPlace.getId())){
            OpGroomPlace _groomPlace = this.findById(groomPlace.getId());
            _groomPlace.setDepict(groomPlace.getDepict());
            _groomPlace.setGroomNum(groomPlace.getGroomNum());
            _groomPlace.setPlaceName(groomPlace.getPlaceName());
            this.updateById(_groomPlace,null);
            return _groomPlace;
        }else{
            groomPlace.setStatus(1);
            this.save(groomPlace);
            return groomPlace;
        }
    }


    /**
     * 批量冻结，批量恢复，
     *
     * @param groomPlaceIds 推荐Id 字符串
     * @param status    1正常  2冻结
     * @return
     */
    public void updateGroomPlaceStatusByIds(String groomPlaceIds, Integer status) {
        OpGroomPlace groomPlace = new OpGroomPlace();
        groomPlace.setStatus(status);
        if(status==3){
            List<OpGroomPlace> groomPlaces = this.queryGroomPlaceListByIds(groomPlaceIds);
            String dataType= "";
            String placeKey= "";
            for(OpGroomPlace _groomPlace:groomPlaces){
                dataType+= "'"+_groomPlace.getDataType()+"',";
                placeKey+= "'"+_groomPlace.getPlaceKey()+"',";
            }
            dataType =StringUtils.subHeadTailString(dataType,",");
            placeKey =StringUtils.subHeadTailString(placeKey,",");
            opGroomDataBiz.deleteWhereSql(" dataType in ("+dataType+") and placeKey in("+placeKey+")");
        }else{
            this.updateByWhereSql(groomPlace, "id in (" + groomPlaceIds + ")");
        }
    }

    /**
     * 推荐位置详情
     * @param groomPlaceId
     * @return
     */
    public OpGroomPlace queryGroomPlaceInfo(BigDecimal groomPlaceId,boolean _groomData){
        //推荐类型，COURSE课程，USER用户、TEACHER老师、ARTICLE资讯，PACKAGE套餐、LIVE直播
        OpGroomPlace groomPlace = this.findById(groomPlaceId);
        if (_groomData){
            OpGroomData groomData = new OpGroomData();
            groomData.setDataType(groomPlace.getDataType());
            if(StringUtils.isNotEmpty(groomPlace.getPlaceKey())){
                groomData.setPlaceKey(groomPlace.getPlaceKey());
                List<OpGroomData> groomDatas = opGroomDataBiz.queryGroomDataList(groomData);
                String dataIds =groomDatas.stream().map(e->e.getDataId().toString()).collect(Collectors.joining(","));
                dataIds=","+dataIds+",";
                groomPlace.setDataIds(dataIds);
                opGroomDataBiz.setGroomDataByGroomPlace(groomDatas);
                //如果课程/直播等数据已删除，则不放入groomDatas中
                for (int i=0;i<groomDatas.size() ;i++){
                    if (groomDatas.get(i).getDataTypeMap() == null){
                        groomDatas.remove(i);
                        i--;
                    }
                }
                groomPlace.setGroomDataList(groomDatas);
            }
        }
        return groomPlace;
    }

    /**
     * 后台批量添加时搜索dataType 为空
     * @param dataType
     * @param status
     * @param isDataType
     * @return
     */
    public List<OpGroomPlace> queryOpGroomPlaceByDataType(String dataType,Integer status,boolean isDataType){
        StringBuilder where = new StringBuilder();
        where.append(" status = ").append(status);
        if (isDataType){
            where.append(" and (dataType='").append(dataType).append("' or dataType='' or dataType is null)");
        }else{
            where.append(" and dataType='").append(dataType).append("'");
        }
        return this.find(where.toString(),null,null);
    }
}
